package com.vote.www.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/4/18.
 */
public class BaseController {

    private final Logger log = LoggerFactory.getLogger(BaseController.class);

    protected void writeJson(HttpServletResponse response, String result) {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(result);
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }
    /**
     * 
     * @author 
     * @date 2017年5月23日 下午2:34:26
     * @param status 1成功0失败
     * @param code	错误码
     * @param msg	信息
     * @param data	数据
     * @param response
     */
    protected void jsonResult(int status,String code, String msg,Object data,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", status);
        map.put("code", code);
        map.put("msg", msg);
        map.put("result", data);
        writeJson(response, JSONObject.toJSONString(map, SerializerFeature.WriteMapNullValue));
    }


}
